package com.kma.summer2012.client.presenters.mainPage;

import com.kma.summer2012.client.CrumbsEventBus;
import com.kma.summer2012.client.events.FillCellTableEvent;
import com.kma.summer2012.client.events.UpdateCellTableEvent;
import com.kma.summer2012.client.presenters.header.HeaderPresenter;
import com.kma.summer2012.client.services.AbstractCallBack;
import com.kma.summer2012.client.services.request.CrumbsRequestService;
import com.kma.summer2012.shared.model.UICrumbsRequest;
import com.kma.summer2012.shared.model.UICrumbsUser;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: EUGENE
 * Date: 3/3/13
 * Time: 12:00 AM
 * To change this template use File | Settings | File Templates.
 */
public class ClosedRequestsPresenter {
    public CrumbsEventBus eventBus = CrumbsEventBus.getInstance();

    public interface Display {
        Integer getTableID();
    }

    private Display display;

    public ClosedRequestsPresenter(Display display) {
        this.display = display;
        bind();
        fillDataGrid(this.display.getTableID());
    }

    public void bind() {
        eventBus.addHandler(UpdateCellTableEvent.getType(), new UpdateCellTableEvent.UpdateCellTableEventHandler() {
            @Override
            public void onFillCellTable(UpdateCellTableEvent event) {
                fillDataGrid(display.getTableID());
            }
        });
    }

    public void fillDataGrid(final Integer tableID) {
        if (display.getTableID().equals(tableID)) {
            UICrumbsUser user = HeaderPresenter.getUser();
            CrumbsRequestService.App.getInstance().getAllClosed(new AbstractCallBack<List<UICrumbsRequest>>() {
                @Override
                public void onSuccess(List<UICrumbsRequest> uiCrumbsRequests) {
                    eventBus.fireEvent(new FillCellTableEvent(uiCrumbsRequests, tableID));
                }
            });
        }
    }
}
